﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Web;
using System.Web.UI.WebControls.WebParts;


namespace Magenic.CodeMastery.SharePoint.PMO
{
    //[Guid("77AE88D7-CA24-4ef7-B475-6D973DD3FFC2")]
    public class ProjectAssignmentsWebPart : WebPart
    {
        private int monthsToRender = 13;

        private String sourceListUrl = String.Empty;

        private System.Web.UI.WebControls.Literal output;

        private StringBuilder debugInfo = new StringBuilder();

        private Exception showEx = null;

        [WebBrowsable(true),
         WebDescription("The # of months to display (1 to 24)"),
         WebDisplayName("Source List URL"),
         Personalizable(PersonalizationScope.Shared)]
        public int MonthsToRender
        {
            get { return monthsToRender; }
            set { monthsToRender = Math.Max(1, Math.Min(24, value)); }
        }

        [WebBrowsable(true),
         WebDescription("The server-relative URL of the source list"),
         WebDisplayName("Source List URL"),
         Personalizable(PersonalizationScope.Shared)]
        public String SourceListUrl
        {
            get { return sourceListUrl; }
            set { sourceListUrl = (value ?? String.Empty).Trim(); }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            output = new System.Web.UI.WebControls.Literal();
            output.ID = "testlit";

            Controls.Add(output);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                GraphComposer graph = new GraphComposer(DateTime.Today, monthsToRender);
                StringBuilder markup = new StringBuilder("<p>Custom Text</p>");
                markup.Append(graph.Render());

                EnsureChildControls();
                output.Text = markup.ToString();
            }
            catch (Exception ex)
            {
                showEx = ex;
            }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {

            if (showEx != null)
            {
                writer.Write(HttpUtility.HtmlEncode(showEx.Message));
                writer.Write("<br/>");
                writer.Write(HttpUtility.HtmlEncode(showEx.StackTrace));
                return;
            }
            if (debugInfo.Length > 0)
            {
            }

            base.Render(writer);
        }

        private delegate void simpleCallback();

        private void runSafely(simpleCallback callBack)
        {
            try
            {
                callBack();
            }
            catch(Exception ex)
            {
                showEx = ex;
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }
        }

        //public override WebPartVerbCollection Verbs
        //{
        //    get
        //    {
        //        WebPartVerbCollection verbs = new WebPartVerbCollection(
        //            base.Verbs, createDemoVerbs());
        //        return verbs;
        //    }
        //}

        //private WebPartVerb[] createDemoVerbs()
        //{
        //    WebPartVerb[] verbs = new WebPartVerb[] {
        //        createClientSideDemoVerb(),
        //        createServerSideDemoVerb()
        //    };
        //    return verbs;
        //}

        //private WebPartVerb createClientSideDemoVerb()
        //{
        //    WebPartVerb verb = new WebPartVerb("SampleVerb_ClientSide",
        //        "javascript:alert(\"Client side verb fired!\");");
        //    verb.Text = "Demo client-side verb";
        //    verb.Description = "This fires a javascript alert box on the client.";
        //    verb.ImageUrl = "/_layouts/images/pndapear.gif";
        //    return verb;
        //}

        //private WebPartVerb createServerSideDemoVerb()
        //{
        //    WebPartVerb verb = new WebPartVerb("SampleVerb_ServerSide",
        //        new WebPartEventHandler(serverSideDemoVerbHandler));
        //    verb.Text = "Demo server-side verb";
        //    verb.Description = "This fires a server-side event handler via postback.";
        //    verb.ImageUrl = "/_layouts/images/portallink.gif";
        //    return verb;
        //}

        //private void serverSideDemoVerbHandler(Object sender, WebPartEventArgs args)
        //{
        ////    throw new Exception("Exception from server-side verb handler");
        //    this.Title = "Changed via server-sice code";
        //}
    }
}